package com.lingkang.flychat.ui.activity.chat;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.StringUtils;
import com.lingkang.flychat.R;
import com.lingkang.flychat.dto.WsMessage;
import com.lingkang.flychat.entity.ChatMessage;
import com.lingkang.flychat.entity.ContactsFriend;
import com.lingkang.flychat.ui.activity.base.BaseActivity;
import com.lingkang.flychat.ui.activity.main.MainActivity;
import com.lingkang.flychat.ui.activity.main.MainActivityInit;
import com.lingkang.flychat.ui.adapter.ChatMessageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FriendChatActivity extends BaseActivity {

    private ContactsFriend contactsFriend = new ContactsFriend();
    @BindView(R.id.tv_header_name)
    TextView tvHeaderName;
    @BindView(R.id.et_content)
    EditText etContent;
    /*    @BindView(R.id.swipe_chat_message)
        SwipeRefreshLayout srChatMessage;*/

    @BindView(R.id.lv_chat_list)
    ListView chatListView;
    private List<ChatMessage> chatMessages = new ArrayList<>();
    private ChatMessageAdapter chatMessageAdapter;

    public Handler getHandler() {
        return handler;
    }

    public Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            int what = msg.what;
            switch (what) {
                case 1:
                    //ListView条目控制在最后一行
                    chatListView.setSelection(chatMessages.size());
                    break;
                case 2:
                    if (msg != null) {
                        ChatMessage chatMessage = new ChatMessage();
                        chatMessage.setMeSend(false);
                        chatMessage.setContent(msg.toString());
                        chatMessages.add(chatMessage);
                        chatMessages.add(chatMessage);
                        //刷新ListView
                        chatMessageAdapter.notifyDataSetChanged();
                        handler.sendEmptyMessage(1);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_chat);
        ButterKnife.bind(this);
        initPo();
        initChatList();
    }

    private void initPo() {
        // 页面传参
        String username = getIntent().getStringExtra("username");
        if (username != null) {
            contactsFriend = MainActivityInit.getContactsFriendByUsername(username);
        } else {
            int childPosition = getIntent().getIntExtra("childPosition", 0);
            int groupPosition = getIntent().getIntExtra("groupPosition", 0);
            contactsFriend = MainActivityInit.childs.get(groupPosition).getContactsFriends().get(childPosition);
        }
        tvHeaderName.setText(contactsFriend.getName());
    }

    @OnClick({R.id.tv_message_send, R.id.iv_header_friend_chat_back})
    void onBtnSendclick(View view) {
        switch (view.getId()) {
            case R.id.tv_message_send:
                sendMsg();
                break;
            case R.id.iv_header_friend_chat_back:
                finish();
                break;
            default:
                break;
        }
    }


    private void sendMsg() {
        String content = etContent.getText().toString();
        if (StringUtils.isEmpty(content))
            return;
        ChatMessage item = new ChatMessage();
        item.setCtm(System.currentTimeMillis());
        item.setMeSend(true);
        item.setName("用户");
        item.setContent(content);
        chatMessages.add(item);
        //刷新ListView
        chatMessageAdapter.notifyDataSetChanged();
        handler.sendEmptyMessage(1);
        WsMessage message = new WsMessage();
        message.setContent(content);
        message.setSendUser(MainActivity.info.getUsername());
        message.setType(1);
        message.setReceiveUser(contactsFriend.getUsername());
        MainActivity.webSocket.send(JSON.toJSONString(message));
        etContent.setText(null);

    }


    private void initChatList() {
        for (int i = 0; i <= 3; i++) {
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.setMeSend(false);
            chatMessage.setContent("你好！" + i);
            chatMessages.add(chatMessage);
        }

        chatMessageAdapter = new ChatMessageAdapter(this, chatMessages);
        chatListView.setAdapter(chatMessageAdapter);

    }
}
